package com.ruoyi.fantasy.service.impl;

import com.huanzhidadi.fantasy.bean.domain.FantasyItem;
import com.huanzhidadi.fantasy.common.constant.Constants;
import com.huanzhidadi.fantasy.common.utils.IdWorker;
import com.ruoyi.common.utils.bean.BeanUtils;
import com.ruoyi.fantasy.domain.FantasyCityMapBO;
import com.ruoyi.fantasy.domain.FantasyItemBO;
import com.ruoyi.fantasy.domain.FantasyItemLogBO;
import com.ruoyi.fantasy.domain.FantasyItemTypeBO;
import com.ruoyi.fantasy.mapper.FantasyItemDao;
import com.ruoyi.fantasy.mapper.FantasyItemLogDao;
import com.ruoyi.fantasy.service.FantasyItemService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
@Slf4j
public class FantasyItemServiceImpl implements FantasyItemService {

    @Resource
    private FantasyItemDao fantasyItemDao;

    @Resource
    private FantasyItemLogDao fantasyItemLogDao;

    @Override
    public List<String> getOldGroundItems() {
        return fantasyItemDao.getOldGroundItems();
    }


    @Override
    public int removeItemByList(List<String> itemIdList) {
        //道具流转记录
        List<FantasyItemLogBO> fantasyItemLogBOList = new ArrayList<>();
        try {
            for (String itemId : itemIdList) {
                //如果道具流转记录中不存在记录，说明该道具只有系统生成记录，在前面的代码中被删掉了
                List<FantasyItemLogBO> list = fantasyItemLogDao.queryLogByItemId(itemId);
                if (list.isEmpty()) {
                    continue;
                }
                FantasyItemLogBO fantasyItemLogBO = new FantasyItemLogBO();
                fantasyItemLogBO.setLogType(Constants.LOG_TYPE_DELETE);
                fantasyItemLogBO.setItemId(itemId);
                fantasyItemLogBO.setLogTime(new Date());
                fantasyItemLogBO.setOwnerId(Constants.Item_OWNER_SYSTEM);
                fantasyItemLogBO.setOwnerType(Constants.Item_OWNER_SYSTEM);
                fantasyItemLogBOList.add(fantasyItemLogBO);
            }
            if (!fantasyItemLogBOList.isEmpty()) {
                fantasyItemLogDao.batchAdd(fantasyItemLogBOList);
            }
            if (!itemIdList.isEmpty()) {
                return fantasyItemDao.removeItemByList(itemIdList);
            }
        } catch (Exception e) {
            log.error("从数据库中删除待删除列表的道具失败", e);
        }
        return 0;
    }


    @Override
    public FantasyItemBO createRandomItem(FantasyItemTypeBO fantasyItemTypeBO, FantasyCityMapBO fantasyCityMapBO) {
        try {
            //创建道具
            FantasyItem fantasyItem = new FantasyItem();
            fantasyItem.setItemType(fantasyItemTypeBO.getId());
            fantasyItem.setOwnerType(Constants.Item_OWNER_SYSTEM);
            fantasyItem.setOwnerId(fantasyCityMapBO.getId());
            fantasyItem.setOwnerTime(new Date());
            fantasyItem.setCreateType(Constants.Item_OWNER_SYSTEM);
            fantasyItem.setCreateId(fantasyCityMapBO.getId());
            fantasyItem.setCreateTime(new Date());

            //填充道具类型的属性
            fantasyItem.setItemName(fantasyItemTypeBO.getName());
            fantasyItem.setImgSrc(fantasyItemTypeBO.getImgsrc());
            fantasyItem.setEquip(fantasyItemTypeBO.getEquip());
            fantasyItem.setPlacement(fantasyItemTypeBO.getPlacement());
            fantasyItem.setUsable(fantasyItemTypeBO.getUsable());

            fantasyItem.setItemId(String.valueOf(IdWorker.nextId()));
            fantasyItem.setCreateTime(new Date());

            fantasyItemDao.addItem(fantasyItem);

            FantasyItemBO fantasyItemBO = new FantasyItemBO();
            BeanUtils.copyProperties(fantasyItem, fantasyItemBO);

            //道具流转记录
            FantasyItemLogBO fantasyItemLogBO = new FantasyItemLogBO();
            fantasyItemLogBO.setLogType(Constants.LOG_TYPE_CREATE);
            fantasyItemLogBO.setItemId(fantasyItem.getItemId());
            fantasyItemLogBO.setLogTime(new Date());
            fantasyItemLogBO.setOwnerId(fantasyCityMapBO.getId());
            fantasyItemLogBO.setOwnerType(Constants.Item_OWNER_SYSTEM);
            fantasyItemLogDao.add(fantasyItemLogBO);

            return fantasyItemBO;
        } catch (Exception e) {
            log.error("创建道具及道具流转记录异常", e);
        }
        return null;
    }

}
